/**
 * 
 */
package cn.edu.jlu.grid.vjm;

import java.util.Vector;

import org.globus.gram.GramAttributes;
import org.globus.gram.GramException;
import org.globus.gram.GramJob;
import org.globus.rsl.ParseException;
import org.ietf.jgss.GSSException;

/**
 * @author whb
 *
 */
public class VJMReservator {
	/* reserve resources, the handler of the reserved resources will be
	 * returned
	 */
	public static int reserve(Vector<Cluster> clusters) {
		int reservationHandler = VJobPool.getInstance().allocateNewReservationHandler();
		VJobPool pool = VJobPool.getInstance();
		
		String basicRsl = 
			"&(executable=$(GLOBUS_LOCATION)/bin/startvjob)" +
			"(stdout = /tmp/startvjob.out)" +
			"(stderr = /tmp/startvjob.err)" +
			"(arguments = -vjcaddress " + 
			VJMCenter.getInstance().getVJCAddress().getHostAddress() +
			" -vjcport " + VJMCenter.getInstance().getVJCPort() +
			" -reservationId " + reservationHandler;

		
		for(int i = 0; i < clusters.size(); i++) {			
			pool.addReservationRequest(reservationHandler, clusters.get(i));
			
			String deltaRsl = " -requestid " + i + ")(count = " + 
				clusters.get(i).vjobCount + ")";
			System.out.println(basicRsl + deltaRsl);
			
			GramJob job = new GramJob(basicRsl + deltaRsl);
			try {
				job.request(clusters.get(i).resourceManagerContact, false, false);
			} catch (GramException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (GSSException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
				
		return reservationHandler;
	}
	public static VJobPool reserve(String rsl) {
		return null;
	}
}
